sdApp.controller("DashController", function ($rootScope, $scope, $state, profileData, ngDialog, $http, transformRequestAsFormPost, fillerDataSrv) {
    $scope.dashVault = {
        title: "DashBoard",
        profile: profileData,
        numOptions: 1,
        newAnswer: "",
        newQuestion: "",
        answers: [],
		email: $rootScope.stateCard.email
    };
	$scope.tabSelected = 'profile';
    if (parseInt($rootScope.stateCard.userId) < 0) {
        $state.go('simplydecide');
    }

	$('.nav.dashboard-nav > li').click(function (e) {
		e.preventDefault();
		$('.nav.dashboard-nav > li').removeClass('active');
		$(this).addClass('active');                
	}); 
		
    var ratioDenominator = $scope.dashVault.profile.decisions.decisionPending +
            $scope.dashVault.profile.decisions.decisionRejected +
            $scope.dashVault.profile.decisions.decisionAccepted;
    $scope.dashVault.pendingRatio = $scope.dashVault.profile.decisions.decisionPending / ratioDenominator;
    $scope.dashVault.rejectedRatio = $scope.dashVault.profile.decisions.decisionRejected / ratioDenominator;
    $scope.dashVault.acceptedRatio = $scope.dashVault.profile.decisions.decisionAccepted / ratioDenominator;
    $scope.progress = 0.75;

    fillerDataSrv.setTopQuestions(profileData.posts.recent_questions);
    fillerDataSrv.setYourVotes(profileData.posts.recent_votes);


    $scope.addAnswer = function () {
        $scope.dashVault.answers.push($scope.dashVault.newAnswer);
        $scope.dashVault.newAnswer = "";
    }

    $scope.removeAnswer = function (idx) {
        $scope.dashVault.answers.splice(idx, 1);
    }

    $scope.postQuestion = function () {
        ngDialog.open({
            template: 'components/popups/postQuestion.html',
            className: 'ngdialog-theme-plain',
            plain: false,
            showClose: true,
            closeByDocument: true,
            closeByEscape: true,
            scope: $scope,
            data: {
                answers: $scope.dashVault.answers,
                question: $scope.dashVault.newQuestion,
                visibility: 0,
                whoCanAnswer: 0,
                days: 0,
                showName: 0,
                graphs: 0,
                categoryid: $rootScope.menuList && $rootScope.menuList[0] && $rootScope.menuList[0].results.id
            }
        });
    }
    $scope.saveQuestion = function (data) {
        console.log(data);
        var request = $http({
            method: "post",
            transformRequest: transformRequestAsFormPost,
            url: $rootScope.urlPrefix + "/question",
            headers: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
                categoryid: data.categoryid,
                question: data.question,
                option: JSON.stringify(data.answers),
                userid: $rootScope.stateCard.userId,
                whocansee: data.visibility,
                whocananswer: data.whoCanAnswer,
                daystodecide: data.days,
                showmyname: data.showName
            }
        });

        // Store the data-dump of the FORM scope.
        request.success(
                function (data) {
                    
                    if (data.result == "success" && data.questionId > -1) {
                       // $scope.closeDialog();
					   ngDialog.close();
                    }
                }
        );
    };

});
